using System;
using System.Collections.Generic;

namespace PS.WebParts.Rollup.Utils
{
    internal static class StringUtil // see Microsoft.SharePoint.Utilities.SPStringUtility
    {
        internal static string GetValueAfterPrefix(string prefixAndValue, string prefix)
        {
            if (!HasPrefix(prefixAndValue, prefix))
            {
                return null;
            }
            if (prefix.Length != prefixAndValue.Length)
            {
                return prefixAndValue.Substring(prefix.Length);
            }
            return string.Empty;
        }

        internal static bool HasPrefix(string prefixAndValue, string prefix)
        {
            if (!string.IsNullOrEmpty(prefix) && !string.IsNullOrEmpty(prefixAndValue))
            {
                if (prefix.Length <= prefixAndValue.Length)
                {
                    if (prefixAndValue.IndexOf(prefix, StringComparison.InvariantCultureIgnoreCase) != 0)
                    {
                        return false;
                    }
                    return true;
                }
                return false;
            }
            return false;
        }

        public static string ArrayToCSV(string[] array)
        {
            string res = "";
            if (array != null)
            {
                if (array.Length > 0)
                {
                    for (int i = 0; i < array.Length - 1; i++)
                    {
                        res += '\'' + array[i] + '\'' + ',';
                    }
                    res += '\'' + array[array.Length - 1] + '\'';
                }
            }

            if(string.IsNullOrEmpty(res))
            {
                res = @"''";
            }

            return res;
        }

        public static string ArrayToCSV(IEnumerable<string> stringCollection)
        {
            List<string> arr = new List<string>(stringCollection);
            return ArrayToCSV(arr.ToArray());
        }
    }
}
